﻿Imports Microsoft.VisualBasic
Imports System.Data
Imports System
Partial Public Class Service
#Region "获取用户信息,通过SESSION"
    Public Function GetUserInfoBySession(ByVal USession As String, ByRef Uinfo As FUserInfo, ByRef ErrString As String) As Boolean
        Try
            Dim sqlstr As String = String.Format("select * from t_user where FSession='{0}'", USession)
            Dim ds As New DataSet
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables.Count < 1 Then
                ErrString = "内部服务器错误"
                Return False
            End If
            If ds.Tables(0).Rows.Count < 1 Then
                ErrString = "验证用户失败!"
                Return False
            End If
            Uinfo = New FUserInfo With {.id = ds.Tables(0).Rows(0).Item("id"), .FName = ds.Tables(0).Rows(0).Item("FName"), .FPass = ds.Tables(0).Rows(0).Item("FPass"), .FRName = ds.Tables(0).Rows(0).Item("FRName"), .FSession = CControl.MD5(Now.ToString & ds.Tables(0).ToString), .FBreathTime = ds.Tables(0).Rows(0).Item("FBreathTime"), .FOrgid = ds.Tables(0).Rows(0).Item("FOrgid")}
            Return True
        Catch ex As Exception
            ErrString = ex.Message
            Return False
        End Try
    End Function
#End Region
#Region "用户登录"
    Public Function _User_Login(ByVal UName As String, ByVal UPass As String, ByRef Uinfo As FUserInfo, ByRef ErrString As String) As Boolean Implements IService._User_Login
        Try
            Dim sqlstr As String = String.Empty
            Dim ds As New DataSet
            If UName.Trim = String.Empty Then
                ErrString = String.Format("登录失败!{0}错误信息:用户名不能为空!", vbCrLf)
                Return False
            End If
            '--加密密码
            If UPass.Trim <> String.Empty Then
                UPass = CControl.MD5(UPass)
            End If
            '--更新数据库
            sqlstr = "update t_user set FPass='' where FPASS IS NULL"
            DControl.ExecSQL(sqlstr)
            '--查询用户
            sqlstr = String.Format("select * from t_User where FName='{0}'", UName.Trim)
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                ErrString = String.Format("登录失败!{0}错误信息:{1}", vbCrLf, ErrString)
                Return False
            End If
            If ds.Tables.Count < 1 Then
                ErrString = String.Format("登录失败!{0}错误信息:内部服务器错误", vbCrLf)
                Return False
            End If
            If ds.Tables(0).Rows.Count < 1 Then
                ErrString = String.Format("登录失败!{0}错误信息:用户名不存在!", vbCrLf)
                Return False
            End If
            '--对比密码
            If ds.Tables(0).Rows(0).Item("FPass") <> UPass Then
                ErrString = String.Format("登录失败!{0}错误信息:密码不正确", vbCrLf)
                Return False
            End If
            '--检查权限
            If DControl.CheckAuth(ds.Tables(0).Rows(0).Item("id"), "SYS", "FLOGIN") = False Then
                ErrString = "当前用户没有系统登录权限"
                Return False
            End If
            '--登录成功
            Dim FUinfo As New FUserInfo With {.id = ds.Tables(0).Rows(0).Item("id"), .FName = ds.Tables(0).Rows(0).Item("FName"), .FPass = String.Empty, .FRName = ds.Tables(0).Rows(0).Item("FRName"), .FSession = CControl.MD5(Now.ToString & ds.Tables(0).ToString), .FOrgid = ds.Tables(0).Rows(0).Item("FOrgid")}
            '--写SESSION记录
            sqlstr = String.Format("update t_User set FSession='{0}',FBreathTime=Getdate() where id={1}", FUinfo.FSession, FUinfo.id)
            If DControl.ExecSQL(sqlstr, ErrString) = False Then
                FUinfo = Nothing
                Return False
            End If
            '--返回信息
            Uinfo = FUinfo
            Return True
        Catch ex As Exception
            ErrString = String.Format("登录失败!{0}错误信息:{1}", vbCrLf, ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "修改本人密码"
    Public Function _User_ModifyMyPass(ByVal USession As String, ByVal FOld As String, ByVal FNew As String, ByRef ErrString As String) As Boolean Implements IService._User_ModifyMyPass
        Try
            If DControl._GetUserBySession(USession, ErrString) = False Then
                ErrString = String.Format("验证用户失败!{0}错误信息:{1}", vbCrLf, ErrString)
                Return False
            End If
            If FOld.Trim = FNew.Trim Then
                ErrString = String.Format("修改密码失败!{0}错误信息:旧密码与新密码不能相同!", vbCrLf)
                Return False
            End If
            '--取用户信息
            Dim FUinfo As New FUserInfo
            If GetUserInfoBySession(USession, FUinfo, ErrString) = False Then
                ErrString = String.Format("修改密码失败!{0}错误信息:{1}", vbCrLf, ErrString)
                Return False
            End If
            If FUinfo Is Nothing Then
                ErrString = String.Format("修改密码失败!{0}错误信息:无法获取用户信息!", vbCrLf)
                Return False
            End If
            '--检查权限
            If DControl.CheckAuth(FUinfo.id, "SYS", "FMODIFYMYPASS") = False Then
                ErrString = "当前用户没有修改密码的权限!"
                Return False
            End If
            '--修改密码
            If FOld <> String.Empty Then
                FOld = CControl.MD5(FOld.Trim)
            End If
            If FNew <> String.Empty Then
                FNew = CControl.MD5(FNew.Trim)
            End If


            If DControl.ExecSQL(String.Format("update t_user set FPass='{0}' where id={1}", FNew, FUinfo.id), ErrString) = False Then
                ErrString = String.Format("修改密码失败!{0}错误信息:{1}", vbCrLf, ErrString)
                Return False
            End If
            Return True
        Catch ex As Exception
            ErrString = String.Format("修改密码失败!{0}错误信息:{1}", vbCrLf, ex.Message)
            Return False
        End Try
    End Function
#End Region
    '#Region "呼吸"
    '    Public Function _User_Breath(ByVal USession As String, ByRef ErrString As String) As Boolean Implements IService._User_Breath
    '        Try
    '            If DControl._GetUserBySession(USession, ErrString) = False Then
    '                ErrString = String.Format("验证用户失败!{0}错误信息:{1}", vbCrLf, ErrString)
    '                Return False
    '            End If
    '            Dim FUinfo As New FUserInfo
    '            If GetUserInfoBySession(USession, FUinfo, ErrString) = False Then
    '                Return False
    '            End If
    '            If FUinfo Is Nothing Then
    '                ErrString = "无法找到用户信息"
    '                Return False
    '            End If
    '            '--写最后呼吸时间 
    '            Dim sqlstr As String = "update t_User set FBreathTime=Getdate() where id=" & FUinfo.id
    '            If DControl.ExecSQL(sqlstr, ErrString) = False Then
    '                Return False
    '            End If
    '            '--清理超时的用户
    '            Dim FCleanTime As DateTime = Now.AddMinutes(-2)
    '            sqlstr = String.Format("update t_User set FBreathTime=NULL,FSession=NULL where FBreathTime<='{0:yyyy-MM-dd HH:mm:ss}'", FCleanTime)
    '            DControl.ExecSQL(sqlstr, ErrString)
    '            Return True
    '        Catch ex As Exception
    '            ErrString = ex.Message
    '            Return False
    '        End Try
    '    End Function
    '#End Region
#Region "检查单项权限"
    Public Function _User_CheckAuth(ByVal USession As String, ByVal FCate As String, ByVal FKey As String, ByRef ErrString As String) As Boolean Implements IService._User_CheckAuth
        If DControl._GetUserBySession(USession, ErrString) = False Then
            ErrString = String.Format("验证用户失败!{0}错误信息:{1}", vbCrLf, ErrString)
            Return False
        End If
        If FCate.Trim = String.Empty Then
            ErrString = "分类名不能为空"
            Return False
        End If
        If FKey.Trim = String.Empty Then
            ErrString = "权限名不能为空"
            Return False
        End If
        Dim FUinfo As New FUserInfo
        If GetUserInfoBySession(USession, FUinfo, ErrString) = False Then
            ErrString = String.Format("检查权限失败!{0}错误信息:{1}", vbCrLf, ErrString)
            Return False
        End If
        Return DControl.CheckAuth(FUinfo.id, FCate.Trim, FKey.Trim)
    End Function
#End Region
#Region "验证用户"
    Private Function FVerifyUser(ByVal FSession As String, ByRef ErrString As String) As Boolean
        If FSession Is Nothing Then
            ErrString = "验证用户失败!"
            Return False
        End If
        '--验证用户
        If DControl._GetUserBySession(FSession, ErrString) = False Then
            ErrString = String.Format("验证用户失败!{0}错误信息:{1}", vbCrLf, ErrString)
            Return False
        End If
        Return True
    End Function
#End Region
#Region "增加用户"
    Public Function _Base_User_AddNew(ByVal FSession As String, ByVal FUinfo As FUserInfo, ByRef ErrString As String) As Boolean Implements IService._Base_User_AddNew
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            Dim sqlstr As String = String.Empty
            Dim ds As New DataSet
            If FUinfo.FName = Nothing Or FUinfo.FRName = Nothing Then
                ErrString = "用户名和姓名不能为空!"
                Return False
            End If
            If FUinfo.FPass = Nothing Then
            Else
                FUinfo.FPass = CControl.MD5(FUinfo.FPass.Trim)
            End If
            If FUinfo.FOrgid = Nothing Then
                ErrString = "用户单位不能为空!"
                Return False
            End If
            '--检查权限
            If _User_CheckAuth(FSession, "SYS", "FMANAGEUSER", ErrString) = False Then
                ErrString = "没有权限"
                Return False
            End If
            '--检查重名
            sqlstr = String.Format("select * from t_user where FName='{0}'", FUinfo.FName.Trim)
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables(0).Rows.Count > 0 Then
                ErrString = "用户名已存在"
                Return False
            End If
            '--增加
            sqlstr = String.Format("insert into t_user(FName,FPass,FRName,FOrgid) values('{0}','{1}','{2}',{3})", FUinfo.FName.Trim, FUinfo.FPass.Trim, FUinfo.FRName.Trim, FUinfo.FOrgid)
            Return DControl.ExecSQL(sqlstr, ErrString)
        Catch ex As Exception
            ErrString = ex.Message
            Return False
        End Try
    End Function
#End Region
#Region "删除用户"
    Public Function Base_User_Delete(ByVal FSession As String, ByVal FUid As String, ByRef ErrString As String) As Boolean Implements IService._Base_User_Delete
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            Dim sqlstr As String = String.Empty
            Dim FUinf As New FUserInfo
            If GetUserInfoBySession(FSession, FUinf, ErrString) = False Then
                Return False
            End If
            If FUinf Is Nothing Then
                ErrString = "验证操作人员信息失败!"
                Return False
            End If
            If _User_CheckAuth(FSession, "SYS", "asdfqwer", ErrString) = False Then
                ErrString = String.Format("没有权限或验证失败!{0}错误信息:{1}", vbCrLf, ErrString)
                Return False
            End If
            If FUid.Trim = Nothing Then
                ErrString = "参数错误!"
                Return False
            End If
            If FUid.Trim = CStr(FUinf.id) Then
                ErrString = "不能删除自己!"
                Return False
            End If
            '--删除
            sqlstr = "delete t_user where id=" & FUid
            Return DControl.ExecSQL(sqlstr, ErrString)
        Catch ex As Exception
            ErrString = String.Format("删除用户失败!{0}错误信息:{1}", vbCrLf, ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "修改用户"
    Public Function _Base_User_ModifyUser(ByVal FSession As String, ByVal FUinfo As FUserInfo, ByVal isPass As Boolean, ByRef ErrString As String) As Boolean Implements IService._Base_User_ModifyUser
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            Dim sqlstr As String = String.Empty
            Dim ds As New DataSet
            If FUinfo.FName = Nothing Or FUinfo.FRName = Nothing Then
                ErrString = "用户名和姓名不能为空!"
                Return False
            End If
            If FUinfo.FPass = Nothing Then
            Else
                FUinfo.FPass = CControl.MD5(FUinfo.FPass.Trim)
            End If
            If FUinfo.id = Nothing Then
                ErrString = "参数不正确"
                Return False
            End If
            '--检查权限
            If _User_CheckAuth(FSession, "SYS", "FMANAGEUSER", ErrString) = False Then
                ErrString = "没有权限"
                Return False
            End If
            '--查询用户是否存在
            sqlstr = "select count(*) from t_user where FName='" & FUinfo.FName & "' and id<>" & FUinfo.id
            ds = New DataSet
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                ErrString = String.Format("修改用户失败!{0}位置:检查用户名是否重复{0}错误信息:{1}", vbCrLf, ErrString)
                Return False
            End If
            If ds.Tables(0).Rows(0).Item(0) > 0 Then
                ErrString = String.Format("修改用户失败!{0}位置:检查用户名是否重复{0}错误信息:新用户名已经存在!", vbCrLf)
                Return False
            End If
            '--修改
            If isPass Then
                '--改密码
                sqlstr = String.Format("update t_user set FName='{0}',FPass='{1}',FRName='{2}',FOrgid={4} where id={3}", FUinfo.FName.Trim, FUinfo.FPass.Trim, FUinfo.FRName.Trim, FUinfo.id, FUinfo.FOrgid)
            Else
                '--不改密码
                sqlstr = String.Format("update t_user set FName='{0}',FRName='{1}',FOrgid={3} where id={2}", FUinfo.FName.Trim, FUinfo.FRName.Trim, FUinfo.id, FUinfo.FOrgid)
            End If
            Return DControl.ExecSQL(sqlstr, ErrString)
        Catch ex As Exception
            ErrString = ex.Message
            Return False
        End Try
    End Function
#End Region
#Region "获取用户列表"
    Public Function _BASE_User_GetUserList(ByVal FSession As String, ByVal FStyle As String, ByRef dsArr As Byte(), ByRef ErrString As String) As Boolean Implements IService._BASE_User_GetUserList
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        '--检查权限
        If _User_CheckAuth(FSession, "SYS", "FMANAGEUSER", ErrString) = False Then
            ErrString = "没有权限"
            Return False
        End If
        Try
            Dim sqlstr As String = String.Empty
            Select Case FStyle
                Case "*"
                    sqlstr = "select t.*,t1.FName from t_user t left join Base_Org t1 on t.FOrgid=t1.id where t.id<>1"
                Case ""
                    sqlstr = "select t.id '编号',t.FName '用户名',t.FPass '密码',t.FRName '姓名',t1.FName '单位' from t_user t left join Base_Org t1 on t.FOrgid=t1.id where t.id<>1"
                Case Else
                    ErrString = "参数错误"
                    Return False
            End Select
            Dim ds As New DataSet
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            dsArr = FControl.CvtDs(ds)
            Return True
        Catch ex As Exception
            ErrString = ex.Message
            Return False
        End Try
    End Function
#End Region
#Region "获取用户权限"
    Public Function _BASE_User_GetUserAuthList(ByVal FSession As String, ByVal FUid As String, ByRef DsArr As Byte(), ByRef ErrString As String) As Boolean Implements IService._BASE_User_GetUserAuthList
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            If _User_CheckAuth(FSession, "SYS", "FMODIFYCONTROL", "") = False Then
                ErrString = "没有权限"
                Return False
            End If
            Dim sqlstr As String = "select t1.id '内码',t1.FCode '编号',t1.FGroup '类别',t1.FKey '权限名',t1.FDescription '权限说明' from t_Auth t left join t_AuthV t1 on t.Faid=t1.id where t.FUid=" & FUid
            Dim ds As New DataSet
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            DsArr = FControl.CvtDs(ds)
            Return True
        Catch ex As Exception
            ErrString = ex.Message
            Return False
        End Try
    End Function
#End Region
#Region "用户分组"
    Public Function _BASE_User_AddNewPair(ByVal FSession As String, ByVal Uid As String, ByVal Vid As String, ByRef ErrString As String) As Boolean Implements IService._BASE_User_AddNewPair
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            If Uid.Trim = String.Empty Or Vid.Trim = String.Empty Then
                ErrString = "分组操作员不能为空"
                Return False
            End If
            Dim FUinf As New FUserInfo
            If GetUserInfoBySession(FSession, FUinf, ErrString) = False Then
                ErrString = String.Format("操作失败!{0}错误信息:{1}", vbCrLf, ErrString)
                Return False
            End If
            If DControl.CheckAuth(FUinf.id, "SYS", "FMANAGEUSERPAIR") = False Then
                ErrString = "没有权限"
                Return False
            End If
            Return DControl.ExecSQL(String.Format("insert into t_Pair(FUid,FVid) values({0},{1})", Uid, Vid), ErrString)
        Catch ex As Exception
            ErrString = ex.Message
            Return False
        End Try
    End Function
#End Region
#Region "获取用户分组列表"
    Public Function _BASE_User_GetPairList(ByVal FSession As String, ByRef TmpArr As Byte(), ByRef ErrString As String) As Boolean Implements IService._BASE_User_GetPairList
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Dim FUinf As New FUserInfo
        If GetUserInfoBySession(FSession, FUinf, ErrString) = False Then
            ErrString = String.Format("操作失败!{0}错误信息:{1}", vbCrLf, ErrString)
            Return False
        End If
        If DControl.CheckAuth(FUinf.id, "SYS", "FMANAGEUSERPAIR") = False Then
            ErrString = "没有权限"
            Return False
        End If
        Dim ds As New DataSet
        If DControl.QuerySQL("select t.id '编号',t1.FName '操作员1',t2.FName '操作员2' from t_Pair t left join t_user t1 on t.FUid=t1.id left join t_user t2 on t.FVid=t2.id", ds, ErrString) = False Then
            Return False
        End If
        TmpArr = FControl.CvtDs(ds)
        Return True
    End Function
#End Region
#Region "删除用户分组列表"
    Public Function _BASE_User_DelPair(ByVal FSession As String, ByVal FPid As String, ByRef ErrString As String) As Boolean Implements IService._BASE_User_DelPair
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            If FPid.Trim = String.Empty Then
                ErrString = "组编码不能为空!"
                Return False
            End If
            Dim FUinf As New FUserInfo
            If GetUserInfoBySession(FSession, FUinf, ErrString) = False Then
                ErrString = String.Format("操作失败!{0}错误信息:{1}", vbCrLf, ErrString)
                Return False
            End If
            If DControl.CheckAuth(FUinf.id, "SYS", "FMANAGEUSERPAIR") = False Then
                ErrString = "没有权限"
                Return False
            End If
            Return DControl.ExecSQL("delete t_Pair where id=" & FPid, ErrString)
        Catch ex As Exception
            ErrString = ex.Message
            Return False
        End Try
    End Function
#End Region
#Region "获取权限列表"
    Public Function _BASE_User_GetAllAuthList(ByVal FSession As String, ByRef DsArr As Byte(), ByRef ErrString As String) As Boolean Implements IService._BASE_User_GetAllAuthList
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            If _User_CheckAuth(FSession, "SYS", "FMODIFYCONTROL", "") = False Then
                ErrString = "没有权限"
                Return False
            End If
            Dim sqlstr As String = "select id '内码',FCode '编号',FGroup '类别',FKey '权限名',FDescription '权限说明' from t_AuthV"
            Dim ds As New DataSet
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            DsArr = FControl.CvtDs(ds)
            Return True
        Catch ex As Exception
            ErrString = ex.Message
            Return False
        End Try
    End Function
#End Region
#Region "授权"
    Public Function _BASE_User_ToAuth(ByVal FSession As String, ByVal FUid As String, ByVal FAuthid As String, ByRef ErrString As String) As Boolean Implements IService._BASE_User_ToAuth
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            If FUid = Nothing Then
                ErrString = "参数不正确!"
                Return False
            End If
            If FAuthid = Nothing Then
                ErrString = "参数不正确"
                Return False
            End If
            Dim tmpstr As String() = Nothing
            tmpstr = Split(FAuthid, "|", -1, CompareMethod.Text)
            Dim sqlstr As String = "delete t_Auth where FUid=" & FUid & ";"
            For i As Integer = 0 To tmpstr.Length - 1
                sqlstr += "insert into t_Auth values(" & FUid & "," & tmpstr(i) & ");"
            Next
            Return DControl.ExecSQL(sqlstr, ErrString)
        Catch ex As Exception
            ErrString = ex.Message
            Return False
        End Try
    End Function
#End Region
#Region "获取用户的关联组"
    Public Function GetUserPair(ByVal uid As String, ByRef FUid() As String, ByRef ErrString As String) As Boolean
        If uid.Trim = String.Empty Then
            Return False
        End If
        Dim sqlstr As String = String.Format("select id from t_User where id in (select fuid from t_Pair where FVid={0}) or id in(select fvid from t_Pair where FUid={0})", uid)
        Dim ds As New DataSet
        '--查ID
        If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
            Return False
        End If
        FUid = New String(ds.Tables(0).Rows.Count) {}
        FUid(0) = uid
        For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
            FUid(i + 1) += ds.Tables(0).Rows(i).Item("id").ToString
        Next
        Return True
    End Function
#End Region

#Region "组织管理_增加新组织"
    Public Function _BASE_Org_AddNew(ByVal FSession As String, ByVal FName As String, ByRef ErrString As String) As Boolean Implements IService._BASE_Org_AddNew
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            '--检查权限---------------------
            Dim FUinf As New FUserInfo
            If GetUserInfoBySession(FSession, FUinf, ErrString) = False Then
                ErrString = String.Format("操作失败!{0}错误信息:{1}", vbCrLf, ErrString)
                Return False
            End If
            If DControl.CheckAuth(FUinf.id, "SYS", "FMANAGEORG") = False Then
                ErrString = "没有权限"
                Return False
            End If
            '---检查权限结束----------------
            If FName.Trim = String.Empty Then
                ErrString = "单位名称不能为空!"
                Return False
            End If
            Dim sqlstr As String = String.Format("select count(id) as a from Base_Org where FName='{0}'", FName.Trim)
            Dim ds As New DataSet
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables(0).Rows(0).Item("a") > 0 Then
                ErrString = "单位名称已经存在"
                Return False
            End If
            sqlstr = String.Format("insert into Base_Org(FName) values('{0}')", FName.Trim)
            Return DControl.ExecSQL(sqlstr, ErrString)
        Catch ex As Exception
            ErrString = ex.Message
            Return False
        End Try
    End Function
#End Region
#Region "组织管理_修改组织"
    Public Function _BASE_Org_ModifyOrg(ByVal FSession As String, ByVal FID As Integer, ByVal FName As String, ByRef ErrString As String) As Boolean Implements IService._BASE_Org_ModifyOrg
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            '--检查权限---------------------
            Dim FUinf As New FUserInfo
            If GetUserInfoBySession(FSession, FUinf, ErrString) = False Then
                ErrString = String.Format("操作失败!{0}错误信息:{1}", vbCrLf, ErrString)
                Return False
            End If
            If DControl.CheckAuth(FUinf.id, "SYS", "FMANAGEORG") = False Then
                ErrString = "没有权限"
                Return False
            End If
            '---检查权限结束----------------
            If FID = Nothing Or FName.Trim = Nothing Then
                ErrString = "参数不正确"
                Return False
            End If
            Dim sqlstr As String = String.Format("select count(id) as a from Base_Org where FName='{0}'", FName.Trim)
            Dim ds As New DataSet
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables(0).Rows(0).Item("a") > 0 Then
                ErrString = "单位名称已经存在"
                Return False
            End If
            Return DControl.ExecSQL(String.Format("update Base_Org set FName='{0}' where id={1}", FName.Trim, FID), ErrString)
        Catch ex As Exception
            ErrString = ex.Message
            Return False
        End Try
    End Function
#End Region
#Region "组织管理_删除组织"
    Public Function _BASE_Org_DelOrg(ByVal FSession As String, ByVal FOid As Integer, ByRef ErrString As String) As Boolean Implements IService._BASE_Org_DelOrg
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            '--检查权限---------------------
            Dim FUinf As New FUserInfo
            If GetUserInfoBySession(FSession, FUinf, ErrString) = False Then
                ErrString = String.Format("操作失败!{0}错误信息:{1}", vbCrLf, ErrString)
                Return False
            End If
            If DControl.CheckAuth(FUinf.id, "SYS", "FMANAGEORG") = False Then
                ErrString = "没有权限"
                Return False
            End If
            '---检查权限结束----------------
            If FOid = Nothing Then
                ErrString = "参数错误"
                Return False
            End If
            Dim sqlstr As String = "select count(*) as a from t_user where FOrgid=" & FOid
            Dim ds As New DataSet
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables(0).Rows(0).Item("a") > 0 Then
                ErrString = "已被使用,无法删除"
                Return False
            End If
            '--水费
            sqlstr = "select count(*) as a from Bill_Water where FOrgid=" & FOid
            ds = New DataSet
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables(0).Rows(0).Item("a") > 0 Then
                ErrString = "已被使用,无法删除"
                Return False
            End If
            '--现金
            sqlstr = "select count(*) as a from Bill_Cash where FOrgid=" & FOid
            ds = New DataSet
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables(0).Rows(0).Item("a") > 0 Then
                ErrString = "已被使用,无法删除"
                Return False
            End If
            '--商品
            sqlstr = "select count(*) as a from Bill_Product where FOrgid=" & FOid
            ds = New DataSet
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables(0).Rows(0).Item("a") > 0 Then
                ErrString = "已被使用,无法删除"
                Return False
            End If
            '--物业
            sqlstr = "select count(*) as a from Bill_Property where FOrgid=" & FOid
            ds = New DataSet
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables(0).Rows(0).Item("a") > 0 Then
                ErrString = "已被使用,无法删除"
                Return False
            End If
            '--删除
            sqlstr = "delete Base_Org where id=" & FOid
            If DControl.ExecSQL(sqlstr, ErrString) = False Then
                ErrString = String.Format("删除单位失败!{0}错误信息:{1}", vbCrLf, ErrString)
                Return False
            End If
            Return True
        Catch ex As Exception
            ErrString = ex.Message
            Return False
        End Try
    End Function
#End Region
#Region "组织管理_获取组织列表"
    Public Function _BASE_Org_GetOrgList(ByVal FSession As String, ByVal FStyle As String, ByRef dsArr As Byte(), ByRef ErrString As String) As Boolean Implements IService._BASE_Org_GetOrgList
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            '--检查权限---------------------
            Dim FUinf As New FUserInfo
            If GetUserInfoBySession(FSession, FUinf, ErrString) = False Then
                ErrString = String.Format("操作失败!{0}错误信息:{1}", vbCrLf, ErrString)
                Return False
            End If
            If DControl.CheckAuth(FUinf.id, "SYS", "FMANAGEORG") = False Then
                ErrString = "没有权限"
                Return False
            End If
            '---检查权限结束----------------
            Dim sqlstr As String = String.Empty
            Dim ds As DataSet = Nothing
            Select Case FStyle.Trim
                Case "*"
                    sqlstr = "select * from Base_Org"
                Case "List"
                    sqlstr = "select id '编号',FName '单位名称' from Base_Org"
                Case Else
                    ErrString = "参数错误"
                    Return False
            End Select
            ds = New DataSet
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            dsArr = FControl.CvtDs(ds)
            Return True
        Catch ex As Exception
            ErrString = ex.Message
            Return False
        End Try
    End Function
#End Region
#Region "组织管理_获取组织名称"
    Public Function _BASE_Org_GetOrgInfo(ByVal FSession As String, ByVal FOid As Integer, ByRef FName As String, ByRef ErrString As String) As Boolean Implements IService._BASE_Org_GetOrgInfo
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            If FOid = Nothing Then
                ErrString = "参数不正确!"
                Return False
            End If
            Dim ds As New DataSet
            If DControl.QuerySQL("select * from Base_Org where id=" & FOid, ds, ErrString) = False Then
                Return False
            End If
            FName = ds.Tables(0).Rows(0).Item("FName")
            Return True
        Catch ex As Exception
            ErrString = ex.Message
            Return False
        End Try
    End Function
#End Region
#Region "组织管理_获取组织名称(直接返回文本)"
    Public Function Base_Org_GetOrgName(ByVal FOid As Integer) As String Implements IService.Base_Org_getOrgName
        Try
            If FOid = Nothing Then
                Return ""
            End If
            Dim ds As New DataSet
            If DControl.QuerySQL("select * from Base_Org where id=" & FOid, ds, "") = False Then
                Return False
            End If
            Return ds.Tables(0).Rows(0).Item("FName")
        Catch ex As Exception
            Return ""
        End Try
    End Function
#End Region
End Class
